package org.example.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.example.domain.entity.*;
import org.example.domain.model.PageData;
import org.example.mapper.FmFrontedCategoryMapper;
import org.example.mapper.FmFrontedShufflingMapper;
import org.example.mapper.ProductMapper;
import org.example.mapper.ProductSkuMapper;
import org.example.prop.OssProperties;
import org.example.service.IFmFrontedGoodsService;
import org.jdom2.internal.ArrayCopy;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class FmFrontedGoodsServiceImpl implements IFmFrontedGoodsService {
    private FmFrontedShufflingMapper fmFrontedShufflingMapper ;
    private FmFrontedCategoryMapper fmFrontedCategoryMapper;
    private ProductMapper productMapper;
    private Log log= LogFactory.getLog(FmFrontedGoodsServiceImpl.class);


    private static final String url="https://fzlbkt.oss-cn-hangzhou.aliyuncs.com/exampledir/";
//    private OssProperties properties;


    public FmFrontedGoodsServiceImpl(FmFrontedShufflingMapper fmFrontedShufflingMapper, FmFrontedCategoryMapper fmFrontedCategoryMapper, ProductMapper productMapper) {
        this.fmFrontedShufflingMapper = fmFrontedShufflingMapper;
        this.fmFrontedCategoryMapper = fmFrontedCategoryMapper;
        this.productMapper = productMapper;
        String[] a=new String[10];

//        this.properties = properties;
    }

    @Override
    public Map<String ,Object> queryShufflingImg(){
        List<Category> categories = fmFrontedCategoryMapper.selectList(null);
        List<IndexShufflingImg> indexShufflingImgs = fmFrontedShufflingMapper.selectList(null);
        List<IndexShufflingImg> indexShufflingImg = indexShufflingImgs.stream().filter(s -> s.getStatus() == 1).collect(Collectors.toList());
        indexShufflingImg.forEach(s->s.setImgUrl(url+s.getImgUrl()));

        List<Category> collect = categories.stream().filter(s -> s.getCategoryLevel() == 1).collect(Collectors.toList());
                collect.forEach(s->{
            s.setCategoryPic(url+s.getCategoryPic());
            s.setCategoryIcon(url+s.getCategoryIcon());
        });
        Map<String ,Object> map=new HashMap<>();
        map.put("category",collect);
        map.put("indexShufflingImg",indexShufflingImg);
        return map;
    }
    @Override
    public  List<Category> queryTwoGoods(String categoryid){
        QueryWrapper<Category> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("parent_id",categoryid);
        List<Category> categoryList = fmFrontedCategoryMapper.selectList(queryWrapper);
        return categoryList;
    }
    @Override
    public PageData<ProductId> queryProduct(Integer page,Integer limit){
        Page<ProductId> productIds=PageHelper.startPage(page,limit);
        productMapper.selectProduct();
        PageData<ProductId> pageData=new PageData<>();
        List<ProductId>result=productIds.getResult();
        result.forEach(s->{
            if (s.getProductSku()!=null&& s.getProductSku().getSkuImg()!=null&&s.getProductSku().getSkuImg()!=""){
                ProductSku productSku = s.getProductSku();
                productSku.setSkuImg(url+productSku.getSkuImg());
                s.setProductSku(productSku);
            }else if (s.getProductImg()!=null&& s.getProductImg().getUrl()!=null&&s.getProductImg().getUrl()!=""){
                ProductImg productImg =s.getProductImg();
                productImg.setUrl(url+productImg.getUrl());
                s.setProductImg(productImg);
            }
        });
        pageData.setTotal(productIds.getTotal());
        pageData.setData(result);
        pageData.setCurrentPage(productIds.getPageNum());
        return pageData;
    }

    @Override
    public PageData<ProductId> queryPickPro(Integer page,Integer categoryId,Integer pagesize){
        Page<ProductId> productIds=PageHelper.startPage(page,pagesize);
        productMapper.selectPickProduct(categoryId);
        PageData<ProductId> pageData=new PageData<>();
        List<ProductId>result=productIds.getResult();

        result.stream().filter(s->s.getCategoryId()==categoryId).forEach(s->{
            if (s.getProductSku()!=null&& s.getProductSku().getSkuImg()!=null&&s.getProductSku().getSkuImg()!=""){
                ProductSku productSku = s.getProductSku();
                productSku.setSkuImg(url+productSku.getSkuImg());
                s.setProductSku(productSku);
            }else if (s.getProductImg()!=null&& s.getProductImg().getUrl()!=null&&s.getProductImg().getUrl()!=""){
                ProductImg productImg =s.getProductImg();
                productImg.setUrl(url+productImg.getUrl());
                s.setProductImg(productImg);
            }
        });
        pageData.setTotal(productIds.getTotal());
        pageData.setData(result);
        pageData.setCurrentPage(productIds.getPageNum());
        return pageData;
    }
    @Override
    public ProductId selectShoppingCartProduct(Integer id){

        ProductId productId = productMapper.selectShoppingCartProduct(id);
        return productId;
    }


}
